package cn.neusoft.controller;

import cn.neusoft.query.BaseQuery;
import cn.neusoft.service.IBaseService;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public class BaseController<T, W> {

    @Autowired
    IBaseService<T> baseService;

    @ApiOperation(value = "保存数据，有id就修改，没id就添加")
    @PostMapping
    public ResultBean save(@RequestBody T t) {
        System.out.println("BaseController-------------save");
        System.out.println(t);
        return baseService.save(t);
    }

    @ApiOperation(value = "通过id删除" )
    @DeleteMapping("/{id}")
    public ResultBean delete(@PathVariable("id") Integer id) {
        System.out.println("BaseController----------delete");
        System.out.println(id);
        return baseService.delete(id);
    }

    @ApiOperation(value = "通过id查询" )
    @GetMapping("/{id}")
    public ResultBean getById(@PathVariable("id") Integer id) {
        System.out.println("BaseController----------getById");
        System.out.println(id);
        return baseService.getById(id);
    }

    @ApiOperation(value = "分页条件搜索" )
    @PostMapping("/all")    // post  /xxx/all
    public <W extends BaseQuery> ResultBean getAll(@RequestBody W baseQuery) {
        System.out.println("BaseController-------getAll");
        return baseService.getAll(baseQuery);
    }

    @ApiOperation(value = "批量删除" )
    @PostMapping("/batch")
    public ResultBean batchDel(@RequestBody List<Integer> ids) {
        System.out.println("BaseController--------------batchDel");
        System.out.println(ids);
        return baseService.batchDel(ids);
    }
}
